<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">组织机构名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" maxlength="100" lay-reqtext="名称不能为空" placeholder="请输入组织机构名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">关联应用</label>
        <div class="layui-input-block">
            <input th:each="aa:${apps}" name="appCodes" id="appCodes" type="checkbox"  th:value="${aa.code}"  th:title="${aa.name}">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级组织机构</label>
        <div class="layui-input-block">
            <input type="hidden" name="pid" id="pid">
            <input type="hidden" name="appCode" id="appCode">
            <input type="text" readonly  id="selectPid"   placeholder="请选择上级组织机构" value="" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序</label>
        <div class="layui-input-block">
            <input type="number" name="orgnazitionSort"    placeholder="请输入排序" value="" class="layui-input" lay-verify="number">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" maxlength="255" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form','layer','tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var index;
        var tree = layui.tree;
        var loadding;
        //监听提交
        form.on('submit(saveBtn)', function (data) {

            var appCodes = [];
            $('input[name=appCodes]:checked').each(function() {
                appCodes.push($(this).val());
            });
            data.field.appCodes=appCodes;
            $.ajax({
                url:"[[@{/orgnazition/save}]]",
                contentType: "application/json; charset=utf-8",
                type:'POST',
                data:JSON.stringify(data.field),
                success:function(data){
                    if(data.code==0) {
                        layer.msg(data.msg);
                        return false;
                    }
                    var index = layer.alert(data.msg, {
                        title: '提示信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);

                    });
                }
            });
            return false;
        });


        $("#selectPid").click(function(){
            loadding = layer.load();
            index = layer.open({
                title: '选择上级组织机构',
                type: 1,
                shade: 0.2,
                maxmin:true,

                area: ['20%', '60%'],
                content: "<div id='apporgnazitionTree'></div>",
                success:function(layero){
                    $.ajax({
                        url:"[[@{/}]]orgnazition/query/tree",
                        contentType: "application/json; charset=utf-8",
                        type:'GET',
                        data:null,
                        success:function(data){
                            if(data.code==1)
                            {
                                //渲染
                                var inst1 = tree.render({
                                    elem: '#apporgnazitionTree'  //绑定元素
                                    ,data: data.data,
                                    click: function(obj){
                                        $("#selectPid").val(obj.data.title);
                                        $("#pid").val(obj.data.id);
                                        $("#appCode").val(obj.data.appCode);
                                        layer.close(index);
                                    }
                                });
                            }
                        },
                        complete:function()
                        {
                            layer.close(loadding);
                        }
                    });

                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });

        })
    });
</script>
</body>
</html>